// 
/**
 * 获取详情保存
 */ 
const bookTmp = require('../model/chapterModel');
const chapterModel = require('../model/chapter');
const superagent = require('superagent');
const cheerio = require('cheerio');
const logger = require('../config/log4');
const { website } = require('../config');

/**
 * 获取书本详情
 */
const getBookDetail = async (chapter) =>{
    if (!chapter){
        chapter = await bookTmp.getOneChapter();//获取一章节
    }
    // var chapter = await bookTmp.getOneChapter();//获取一章节
    if (chapter){
        superagent
            .get(`${website.baseurl}/${chapter.url}`)
            .set('Accept',' text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8')
            .set('Accept-Encoding','gzip,deflate')
            .set('Connection','keep-alive')
            .end((err, res) => {
                if (err){
                    setTimeout(() => {
                        getBookDetail(chapter);
                    }, 10000);
                    logger.error(err);
                    return;
                    // throw err;
                }
                const $ = cheerio.load(res.text);
                // if ($('.title')[0].children[0].data.indexOf('检测到访问行为异常') >= 0){
                //     logger.error('反爬虫');
                //     process.exit();
                // }
                const params = {
                    bookId: chapter.bookId,
                    chpaterId: chapter.url.split('/')[3].split('.')[0],
                    chapterName: Array.isArray(chapter.chapterName) ? chapter.chapterName[0] : chapter.chapterName,
                    chapterTxt: $("#content").text()
                }
                chapterModel.save(params);
                logger.info('保存章节');
                getBookDetail();
            })
        logger.info('保存一章节')
    }else{
        //程序结束
        //爬虫结束
        logger.info('爬虫结束 程序退出')
        process.exit();
    }
}
module.exports = {
    getBookDetail
}